//
//  XjhzVC+UI.swift
//  Auscultation
//
//  Created by 来 on 2020/12/8.
//

import Foundation

extension XjhzVC {
    
    func initUI() {
        
        let top_view = UIImageView.init(image: #imageLiteral(resourceName: "beijing3@1.5x"))
        view.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(UIDevice.lgf_NavBarH())
        }
        self.navTitle = "新建患者"
        self.lgf_NavigationBar.lgf_Title?.textColor = UIColor.white
        
        scrollView = UIScrollView.lgf_Table("FFFFFF")
        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(top_view.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(0)
        }
        
        tx = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 0, placeholder: "", text: "", true)
        tx.lgf_ViewName = "头像"
        tx.delegate = self
        scrollView.addSubview(tx)
        tx.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let label0 = UILabel.init()
        label0.text = "头像"
        label0.textColor = UIColor.init(lgf_HexString: "222222")
        label0.font = UIFont.boldSystemFont(ofSize: 16)
        tx.addSubview(label0)
        label0.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        tx_i = UIImageView.init(image: nil)
        tx_i.backgroundColor = UIColor.init(lgf_HexString: "F0F0F0")
        tx_i.layer.cornerRadius = 20
        tx_i.clipsToBounds = true
        tx_i.lgf_ViewName = ""
        tx.addSubview(tx_i)
        tx_i.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-30)
            make.width.height.equalTo(40)
        }
        
        // 姓名
        xm = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "患者姓名", text: "", false)
        xm.delegate = self
        scrollView.addSubview(xm)
        xm.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(tx.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let label1 = UILabel.init()
        label1.text = "姓名"
        label1.textColor = UIColor.init(lgf_HexString: "222222")
        label1.font = UIFont.boldSystemFont(ofSize: 16)
        xm.addSubview(label1)
        label1.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        let xm_x = UILabel.init()
        xm_x.text = "*"
        xm_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        xm_x.font = UIFont.boldSystemFont(ofSize: 16)
        xm.addSubview(xm_x)
        xm_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        // 性别
        xb = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 80, placeholder: "患者性别", text: "", true)
        xb.lgf_ViewName = "性别"
        xb.delegate = self
        scrollView.addSubview(xb)
        xb.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(xm.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let label2 = UILabel.init()
        label2.text = "性别"
        label2.textColor = UIColor.init(lgf_HexString: "222222")
        label2.font = UIFont.boldSystemFont(ofSize: 16)
        xb.addSubview(label2)
        label2.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        let xb_x = UILabel.init()
        xb_x.text = "*"
        xb_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        xb_x.font = UIFont.boldSystemFont(ofSize: 16)
        xb.addSubview(xb_x)
        xb_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        // 监护人
        jhr = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "监护人姓名", text: "", false)
        jhr.delegate = self
        scrollView.addSubview(jhr)
        jhr.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(xb.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let jhr_l = UILabel.init()
        jhr_l.text = "监护人"
        jhr_l.textColor = UIColor.init(lgf_HexString: "222222")
        jhr_l.font = UIFont.boldSystemFont(ofSize: 16)
        jhr.addSubview(jhr_l)
        jhr_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        let jhr_x = UILabel.init()
        jhr_x.text = "*"
        jhr_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        jhr_x.font = UIFont.boldSystemFont(ofSize: 16)
        jhr.addSubview(jhr_x)
        jhr_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        // 手机号
        sjh = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "11位手机号", text: "", false)
        sjh.delegate = self
        scrollView.addSubview(sjh)
        sjh.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(jhr.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let label8 = UILabel.init()
        label8.text = "手机号"
        label8.textColor = UIColor.init(lgf_HexString: "222222")
        label8.font = UIFont.boldSystemFont(ofSize: 16)
        sjh.addSubview(label8)
        label8.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        let sjh_x = UILabel.init()
        sjh_x.text = "*"
        sjh_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        sjh_x.font = UIFont.boldSystemFont(ofSize: 16)
        sjh.addSubview(sjh_x)
        sjh_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        // 身份证
        sfz = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "18位身份证号", text: "", false)
        sfz.delegate = self
        scrollView.addSubview(sfz)
        sfz.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(sjh.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let label9 = UILabel.init()
        label9.text = "身份证"
        label9.textColor = UIColor.init(lgf_HexString: "222222")
        label9.font = UIFont.boldSystemFont(ofSize: 16)
        sfz.addSubview(label9)
        label9.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
//        let sfz_x = UILabel.init()
//        sfz_x.text = "*"
//        sfz_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
//        sfz_x.font = UIFont.boldSystemFont(ofSize: 16)
//        sfz.addSubview(sfz_x)
//        sfz_x.snp.makeConstraints { (make) in
//            make.left.equalToSuperview().offset(-10)
//            make.centerY.equalToSuperview().offset(2)
//        }
        
        // 出生日期
        csrq = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 80, placeholder: "出生年月日", text: "", true)
        csrq.lgf_ViewName = "出生日期"
        csrq.delegate = self
        scrollView.addSubview(csrq)
        csrq.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(sfz.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let label3 = UILabel.init()
        label3.text = "出生日期"
        label3.textColor = UIColor.init(lgf_HexString: "222222")
        label3.font = UIFont.boldSystemFont(ofSize: 16)
        csrq.addSubview(label3)
        label3.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        let csrq_x = UILabel.init()
        csrq_x.text = "*"
        csrq_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        csrq_x.font = UIFont.boldSystemFont(ofSize: 16)
        csrq.addSubview(csrq_x)
        csrq_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        // 地区
        dq = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 80, placeholder: "地区信息", text: "", true)
        dq.lgf_ViewName = "地区"
        dq.delegate = self
        scrollView.addSubview(dq)
        dq.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(csrq.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let label6 = UILabel.init()
        label6.text = "地区"
        label6.textColor = UIColor.init(lgf_HexString: "222222")
        label6.font = UIFont.boldSystemFont(ofSize: 16)
        dq.addSubview(label6)
        label6.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
//        let dq_x = UILabel.init()
//        dq_x.text = "*"
//        dq_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
//        dq_x.font = UIFont.boldSystemFont(ofSize: 16)
//        dq.addSubview(dq_x)
//        dq_x.snp.makeConstraints { (make) in
//            make.left.equalToSuperview().offset(-10)
//            make.centerY.equalToSuperview().offset(2)
//        }
        
        // 详细地址
        xxdz = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "街道信息", text: "", false)
        xxdz.delegate = self
        scrollView.addSubview(xxdz)
        xxdz.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(dq.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let label7 = UILabel.init()
        label7.text = "详细地址"
        label7.textColor = UIColor.init(lgf_HexString: "222222")
        label7.font = UIFont.boldSystemFont(ofSize: 16)
        xxdz.addSubview(label7)
        label7.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        let button1 = UIButton.init(type: .custom)
        button1.backgroundColor = UIColor.init(lgf_HexString: EWEDarkMode.c_0F88F2)!
        button1.setTitle("提交", for: .normal)
        button1.setTitleColor(EWEDarkMode.white(), for: .normal)
        button1.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button1.layer.cornerRadius = 4
        button1.layer.masksToBounds = true
        button1.addTarget(self, action: #selector(XjhzVC.tijiao(_:)), for: .touchUpInside)
        scrollView.addSubview(button1)
        button1.snp.makeConstraints { (make) in
            make.top.equalTo(xxdz.snp.bottom).offset(50)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().offset(-75)
            make.height.equalTo(44)
        }
        
        scrollView.lgf_AutoContentSize(0)
    }
    
}

extension XjhzVC: TZImagePickerControllerDelegate {
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool, infos: [[AnyHashable : Any]]!) {
        EWERequest.uploadFile(photos, "0", "image") { [weak self] (url) in
            self?.tx_i.lgf_setImage(url)
            self?.tx.lgf_Name = url
        } _: { (error) in
            SVProgressHUD.showError(error)
        }

    }
}

extension XjhzVC: LGFLoginImageTitleViewDelegate {
    func selectLGFLoginImageTitleView(_ view: LGFLoginImageTitleView) {
        self.view.endEditing(true)
        if view.lgf_ViewName == "性别" {
            let picker = LGFBottomPickerView()
            let data = ["男", "女"]
            let sindex = data.lgf_IndexOfObject(self.xb.textField.text ?? "男")
            picker.showIn(self.view, data, sindex) { [weak self] (view, name, index) in
                self?.xb.text = data[index]
                self?.xb.lgf_Name = (index + 1).description
            }
        } else if view.lgf_ViewName == "出生日期" {
            let picker = LGFBottomDatePickerView()
            picker.showIn(self.view, .date, "yyyy-MM-dd") { (view, date, timestamp) in
                self.csrq.text = timestamp.description.lgf_TimeStampToDate().lgf_TimeStr("yyyy-MM-dd")
                self.csrq.lgf_Name = timestamp.description
            }
        } else if view.lgf_ViewName == "地区" {
            LGFAddressPickerView().showIn(self.view) { [weak self] (view, name, adddress) in
                self?.dq.text = adddress
            }
        } else if view.lgf_ViewName == "头像" {
            let imagePickerController =  TZImagePickerController.init(maxImagesCount: 1, columnNumber: 1, delegate: self, pushPhotoPickerVc: true)
            imagePickerController?.allowPickingVideo = false
            imagePickerController?.allowPickingGif = false
            UIApplication.shared.keyWindow?.rootViewController!.present(imagePickerController!, animated: true, completion: nil)
        }
    }
}
